package controller 
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.Security;
	import model.GameData;
	import view.popup.NoticePopup;
	/**
	 * ...
	 * @author bimkute
	 */
	public class GameController extends EventDispatcher
	{
		private static var instance:GameController;
		private var arrSwf:Array = [];
		public var percent:int = 0;
		private var countSwf:int = 0;
		private var byteLoaded:Number = 0;
		private var loading:Boolean;
		private var popup:NoticePopup;
		
		public function GameController() 
		{
			
		}
		
		public static function getInstance():GameController 
		{
			if (!instance) 
			{
				instance = new GameController();
			}
			return instance;
		}
		
		public function loadSwf(str:String, nameSwf:String, preload:Boolean = false):void 
		{
			
			Security.allowDomain("*");
			Security.allowInsecureDomain("*");
			var context:LoaderContext = new LoaderContext();
			context.checkPolicyFile = true;
			context.allowCodeImport = true;
			context.applicationDomain = ApplicationDomain.currentDomain;
			
			loading = preload;
			
			var urlReg:URLRequest = new URLRequest(str);
			var loader:Loader = new Loader();
			
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteLoadSwf);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
			
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIoerror);
			loader.load(urlReg, context);
			
			arrSwf[nameSwf] = loader;
			
			trace("====+++=======", arrSwf.length)
		}
		
		private function onCompleteLoadSwf(e:Event):void 
		{
			var loader:Loader = e.target.loader;
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onCompleteLoadSwf);
			loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
			loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onIoerror);
			
			if (!loading) 
			{
				//byteLoaded = GameData.getInstance().sizeSwf[countSwf];
				countSwf++;
				//trace("tong file da load dc:=====================================", byteLoaded)
			}
			else 
			{
				dispatchEvent(new Event("loadingComplete"));
			}
			
			trace(GameData.getInstance().arrLoad.length , countSwf)
			if (GameData.getInstance().arrLoad.length == countSwf) 
			{
				dispatchEvent(new Event("loadComplete"));
			}
			
		}
		
		private function onProgress(e:ProgressEvent):void 
		{
			var totalLoad:int = GameData.getInstance().arrLoad.length;
			/*trace("byteloaded: ", byteLoaded, "=====")
			trace("***************: ", e.bytesLoaded, "=====")
			trace("***************: ", e.bytesTotal, "********")*/
			if (e.bytesLoaded == e.bytesTotal) 
			{
				byteLoaded += e.bytesTotal;
				
				//trace("da dc cong them vao tong", byteLoaded)
			}
			if (Math.ceil(((byteLoaded + e.bytesLoaded) / GameData.getInstance().sizeSwf[totalLoad]) * 100) <= 100) 
			{
				percent = Math.ceil(((byteLoaded + e.bytesLoaded) / GameData.getInstance().sizeSwf[totalLoad]) * 100);
			}
			else 
			{
				percent = 100;
			}
			
			trace("------------: ", percent, "=====")
			
			//percent = int((e.bytesLoaded / e.bytesTotal) * 100);
			
		}
		
		private function onIoerror(e:IOErrorEvent):void 
		{
			trace("load error")
		}
		
		public function getClass(nameClass:String, nameSwf:String):MovieClip 
		{
			trace("check thang dc add: ", nameClass, nameSwf)
			var myClass:Class = arrSwf[nameSwf].contentLoaderInfo.applicationDomain.getDefinition(nameClass) as Class;
			var mc:MovieClip = new myClass();
			trace(mc.numChildren, "^6^", nameClass, nameSwf);
			return mc;
		}
		
	}

}